package com.fragrance.simboss;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.fragrance.service.SimBossService;
import com.fragrance.service.impl.SimBossServiceImpl;

/**
 * @author zhilin
 * 
 */
@Deprecated
public class SimBossUtils {
	
	private static final String SUCCESS_CODE = "0";
	
	public final static String BASE_DOMAIN_URL = "https://api.simboss.com";
	
	public final static String APPID = "102420132776";
	
	public final static String APPSECRET ="5f45c59f4961649f23ae2f1d545e9d4c";
	
	 private final static Logger logger = LoggerFactory.getLogger(SimBossUtils.class);
	
	public static String calcSign(Map<String, String> params, String secret) {
		params = new TreeMap<String, String>(params);
		StringBuilder stringBuilder = new StringBuilder();
		int size = params.size();
		int index = 0;
		for (Map.Entry<String, String> entry : params.entrySet()) {
			index++;
			stringBuilder.append(entry.getKey()).append("=")
					.append(entry.getValue());
			if (index < size) {
				stringBuilder.append("&");
			}
		}
		stringBuilder.append(secret);
		return DigestUtils.sha256Hex(stringBuilder.toString());
	}

	public static String post(String uri, Map<String, String> params) {
		CloseableHttpResponse response = null;
		CloseableHttpClient httpclient = null;
		String result = null;
		try {
			HttpPost httpPost = new HttpPost(uri);
			RequestConfig requestConfig = RequestConfig.DEFAULT;
			httpPost.setConfig(requestConfig);
			List<BasicNameValuePair> paramList = new ArrayList<BasicNameValuePair>();
			for (Map.Entry<String, String> entry : params.entrySet()) {
				paramList.add(new BasicNameValuePair(entry.getKey(), entry
						.getValue()));
			}
			UrlEncodedFormEntity paramEntity = new UrlEncodedFormEntity(
					paramList, "utf-8");
			httpPost.setEntity(paramEntity);
			httpclient = HttpClients.createDefault();
			response = httpclient.execute(httpPost);
			HttpEntity resultEntity = response.getEntity();
			result = EntityUtils.toString(resultEntity);
			EntityUtils.consume(resultEntity);
		} catch (Exception e) {
			logger.error("发生异常", e);
		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {
				}
			}
			if (httpclient != null) {
				try {
					httpclient.close();
				} catch (IOException e) {
				}
			}
		}
		return result;
	}
	
	public static boolean checkSuccess(JSONObject jobj){
		if(jobj == null){
			return false;
		}
		if(SUCCESS_CODE.equals(jobj.getString("code"))){
			return true;
		}
		return false;
	}
	
	public static void main(String[] args) {
		SimBossService ss = new SimBossServiceImpl();
		System.out.println(ss.getAccountInfo());
	
	}
}
